// Created by WXX on 2021/12/24 19:49
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

/**
 * 执行用时：4 ms, 在所有 C++ 提交中击败了90.27%的用户
 * 内存消耗：5.8 MB, 在所有 C++ 提交中击败了85.65%的用户
 */
class Solution {
public:
    int ans = 0;

    int numTilePossibilities(string s) {
        sort(s.begin(), s.end());
        return dfs(s, 0) - 1;
    }

    int dfs(string &s, int u) {
        int res = 1;
        if (u == s.size()) return res;

        for (int i = 0; i < s.size(); i++) {
            if (i && s[i] == s[i - 1]) continue;
            if (s[i] == ' ') continue;
            char c = s[i];
            s[i] = ' ';
            res += dfs(s, u + 1);
            s[i] = c;
        }
        return res;
    }
};

int main() {

    cout << Solution().numTilePossibilities("AAB") << endl;  // 8

    return 0;
}
